#include("/common/_layout.html")
#@layout()
#define layoutHead()
	<title>课程编辑</title>
#end
#define layoutBody()
	<body>
		<div class="page-content-wrap">
				<form class="layui-form" lay-filter="formedit">
					<input type="hidden" name="course_id">
					<div class="layui-tab" style="margin: 0;">
						<ul class="layui-tab-title">
							<li><a id="a_goback_url" href="javascript:window.history.back();">课程列表</a></li>
							<li class="layui-this">课程编辑</li>
						</ul>
						<div class="layui-tab-content">
							<div class="layui-tab-item"></div>
							<div class="layui-tab-item layui-show">
								<div class="layui-form-item">
									<label class="layui-form-label">课程名称：</label>
									<div class="layui-input-block">
										<input type="text" name="course_name" required lay-verify="required" placeholder="请输入课程名称" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">主讲老师：</label>
									<div class="layui-input-block">
										<input type="text" name="lecturer_name" required lay-verify="required" placeholder="请输入主讲老师" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">关联栏目：</label>
									<div class="layui-inline">
										<select id="article_catid" name="article_catid" class="layui-input layui-unselect">
											<option value="">无关联栏目</option>
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">课程类型：</label>
									<div class="layui-inline">
										<select id="course_typeid" name="course_typeid" lay-verify="required" class="layui-input layui-unselect">
											<option value="1">教材</option>
											<option value="2">主题</option>
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">授课对象：</label>
									<div class="layui-inline">
										<select id="prelect_object_id" name="prelect_object_id" lay-verify="required">
											<option value="1">学生</option>
											<option value="2">老师</option>
											<option value="3">家长</option>
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">适合年级：</label>
									<div class="layui-inline">
										<select id="grade_id" name="grade_id" lay-verify="required">
											<option value="1">托班</option>
											<option value="2">小班</option>
											<option value="3">中班</option>
											<option value="4">大班</option>
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">科目：</label>
									<div class="layui-inline">
										<select id="subject_id" name="subject_id" lay-verify="required">
											<option value="1">语文</option>
											<option value="2">数学</option>
											<option value="3">英语</option>
											<option value="0">其他</option>
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">标签：</label>
									<div class="layui-input-block">
										<input type="text" name="course_tab" placeholder="" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">封面：</label>
									<div class="layui-input-block">
										<input type="hidden" id="front_cover_url" name="front_cover_url">
										<input type="file" name="file_front_cover_url" lay-ext="jpeg|jpg|png|gif" lay-type="images" lay-title="上传图片" class="layui-upload-file">
										<div id="div_front_cover_url" style="padding: 5px;"></div>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">课程简介：</label>
									<div class="layui-input-block">
										<textarea class="layui-textarea layui-hide" id="course_desc" name="course_desc" lay-verify="content"></textarea>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">发布：</label>
									<div class="layui-input-block">
										<input type="radio" name="power_id" value="1" title="公开" lay-skin="primary">
										<input type="radio" name="power_id" value="0" title="私有" lay-skin="primary">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">状态：</label>
									<div class="layui-inline">
										<select name="course_state" lay-verify="required">
											<option value="1">启用</option>
											<option value="0">禁用</option>
										</select>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="layui-form-item" style="padding-left: 10px;">
						<div class="layui-input-block">
							<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
							
						</div>
					</div>
				</form>
		</div>
		<script src="/static/js/jquery.formHelp.js" type="text/javascript" charset="utf-8"></script>
		<script>
			var course_desc_editor_idx;
			
			$('#prelect_object_id').val(getUrlParam('poid'));
			
			layui.use(['form', 'layer', 'laytpl', 'element', 'upload', 'layedit'], function() {
				var form = layui.form();
				var layer = layui.layer;
				var laytpl = layui.laytpl,
					layedit = layui.layedit,
					element = layui.element();			
				
				layui.upload({
					url: '/api/file/upload',
					success: function(res) {
						if (res.code == 0){
							var sourceId = res.data.paramid.replace("file_","");
							var ftype = $('input[name="' + res.data.paramid+'"]').attr("lay-type");
							
							$('#'+sourceId).val(res.data.src);
							
							formatUpFile(sourceId, res.data.src, ftype);
						}else{
							parent.alertMsg(res.msg);
						}
					}
				});
				
				// 编辑器图片上传,放在build前面
				layedit.set({
				  uploadImage: {
					url: '/api/file/upload' 
					,type: '' //默认post
				  }
				});
				
				function formatUpFile(elid, surl, ftype){
					if (surl == null || surl == "" || surl == "undefined"){
						return;
					}
					var shtml = "";
					if (ftype=="images"){
						shtml = '<img src="'+surl+'" width="100" height="100">';
					}else{
						shtml = '<a href="'+surl+'" target="_blank">'+surl.substr(surl.lastIndexOf('/')+1)+'</a>';
					}
					$('#div_'+elid).html(shtml);
				}
				
				function initDescEditor(){
					//创建一个编辑器
					course_desc_editor_idx = layedit.build('course_desc', {
						tool: ['strong' //加粗
							, 'italic' //斜体
							, 'underline' //下划线
							, 'del' //删除线
							, '|' //分割线
							, 'left' //左对齐
							, 'center' //居中对齐
							, 'right' //右对齐
							, 'link' //超链接
							, 'unlink' //清除链接
							, 'image' //插入图片
						],
						height: 100
					});
				}
				
				// 表单验证
				form.verify({
				});

				//监听提交
				form.on('submit(formDemo)', function(formData){
					
					formData.field.course_desc = layedit.getContent(course_desc_editor_idx);
					
					sendPost("/api/course/save", formData.field, function(res) {
						layer.alert(res.msg, {
							title: '信息提示'
						}, function(idx){
							if (res.code==200){
								window.history.back();;
							}
							layer.close(idx);
						});
					});
					return false;
				});
				
				function bindSelectData(listdata, pid, level){
					var rows = listdata.filter(function (e) { return e.parent_id == pid && (e.link_course === 1 || e.link_course === true); });
					for (var i = 0; i < rows.length; i++) {
						var shtml = '<option value="'+rows[i].cat_id+'">';
						for(j=0;j<level;j++){
							shtml += '&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						if (level>0){
							shtml += '├─';
						}
						shtml += rows[i].cat_name + '</option>';
						document.getElementById('article_catid').innerHTML += shtml;
						// 迭代
						bindSelectData(listdata, rows[i].cat_id, level+1);
					}
				}

				// 页面初始化
				function page_init(){
					var id = getUrlParam('id');

					// 加载初始化编辑数据
					sendGet("/api/course/getOne", {
						id : getUrlParam("id")
					}, function(res) {
						//console.log(res);
						if (res.hasOwnProperty("listcat")) {
							//console.log(res.listcat);
							bindSelectData(res.listcat, 0, 0);
							form.render('select');
						}
						// 初始化编辑数据
						if (res.hasOwnProperty("oneinfo")) {
							$("form.layui-form").setForm(res.oneinfo);
							formatUpFile("front_cover_url", res.oneinfo.front_cover_url, "images");
							initDescEditor();
						}
						form.render();
					});
				}
				
				initDescEditor();
				page_init();
			});
			
			
					
		</script>
	</body>

#end